# from django.shortcuts import render
from rest_framework.views import APIView
from django.conf import settings
import json
from urllib.parse import urlencode
from urllib.request import urlopen
from rest_framework.response import Response
from rest_framework import status

class CaptchaAPIView(APIView):
    def get(self,request):
        """验证码的验证结果校验"""
        AppSecretKey = settings.TENCENT_CAPTCHA["App_Secret_Key"]
        appid = settings.TENCENT_CAPTCHA["APPID"]
        Ticket = request.query_params.get("ticket")
        Randstr = request.query_params.get("randstr")
        UserIP = request._request.META.get("REMOTE_ADDR")
        print("用户ID地址：%s" % UserIP)
        params = {
            "aid": appid,
            "AppSecretKey": AppSecretKey,
            "Ticket": Ticket,
            "Randstr": Randstr,
            "UserIP": UserIP
        }
        params = urlencode(params)

        f = urlopen("%s?%s" % (settings.TENCENT_CAPTCHA["GATEWAY"], params))
        content = f.read()
        res = json.loads(content)
        print(res)
        if res:
            error_code = res["response"]
            if int(error_code) == 1:
                return Response("验证通过！")
            else:
                return Response("验证失败！%s" % res["err_msg"], status=status.HTTP_400_BAD_REQUEST)
        else:
            return Response("验证失败！", status=status.HTTP_400_BAD_REQUEST)


# from luffyapi.libs.yuntongxun.sms import CCP
import random
from django_redis import get_redis_connection
from luffyapi.settings import constants
from mycelery.sms.tasks import send_sms
class SMSCodeAPIView(APIView):
    '''
    短信验证码
    '''
    def get(self,request,mobile):
        """
               短信验证码
               """
        redis_conn = get_redis_connection('sms_code')

        # 手机号是否处于发送短信的冷却时间内
        interval = redis_conn.get("sms_time_%s" % mobile)
        if interval is not None:
            return Response("不能频繁发送短信！")

        #生成短信验证阿妈
        sms_code = "%06d" % random.randint(0,999999)

        #保存短信验证码与发送记录
        #使用redis提供的管道操作可以一次性执行多条redis命令
        p1 = redis_conn.pipeline()
        p1.multi()
        p1.setex("sms_%s"%mobile,constants.SMS_CODE_EXPIRE,sms_code)
        #设置短信有效期
        p1.setex("sms_time_%s" % mobile,constants.SMS_CODE_INTERVAL,"_")
        #设置发送短信间隔为60s
        p1.execute()

        # #发送短信验证码
        # ccp = CCP()
        # ccp.send_template_sms(mobile,[sms_code,constants.SMS_CODE_EXPIRE//60],settings.SMS.get('_templateID'))
        send_sms.delay(mobile,sms_code)

        return Response({'message':'OK'},status.HTTP_200_OK)

from rest_framework.generics import CreateAPIView
from .models import User
from .serializers import UserModelSerializer
class UserCreateAPIView(CreateAPIView):
    queryset = User.objects.all()
    serializer_class = UserModelSerializer